﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class UserList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindUserGroup();
                BindGridView();
            }
        } 

        #region Private Method

        private void BindUserGroup()
        {
            FunctionGroupService functionGroupServiceObj = ServiceFactory.getFunctionGroupService();
            int totalPage = 0;
            OperationResult<List<tb_FunctionGroup>> result = functionGroupServiceObj.getAllFunctionList(1, 10, out   totalPage);

            if (result.operationSuccess)
            {
                dlGroup.DataSource = result.obj;
                dlGroup.DataTextField = "GroupName";
                dlGroup.DataValueField = "Id";
                dlGroup.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindGridView()
        {
            UserService userServiceObj = ServiceFactory.getUserService();
            OperationResult<List<tb_User>> result = userServiceObj.GetAllUserList(1, 25);

            if (result.operationSuccess)
            {
                gvUserList.DataSource = result.obj;
                gvUserList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            UserService userServiceObj = ServiceFactory.getUserService();

            FunctionGroupService functionGroupServiceObj = ServiceFactory.getFunctionGroupService();
            int totalPage = 0;
            OperationResult<List<tb_FunctionGroup>> result = functionGroupServiceObj.getAllFunctionList(1, 10, out   totalPage);

            if (result.operationSuccess)
            {
                dlEditGroup.DataSource = result.obj;
                dlEditGroup.DataTextField = "GroupName";
                dlEditGroup.DataValueField = "Id";
                dlEditGroup.DataBind();
            }
            else
            {
                //EXCEPTION 
                //return null;
            }

            OperationResult<tb_User> resultEdit = userServiceObj.GetUserById(id);
            if (result.operationSuccess == true)
            {
                txtEditName.Text = resultEdit.obj.Name;
                txtEditCNName.Text = resultEdit.obj.CNName;
                txtEditPWD.Text = resultEdit.obj.PWD;
                txtEditTel.Text = resultEdit.obj.Tel; 
                txtEditDescription.Text = resultEdit.obj.Description;
                dlEditGroup.SelectedValue = resultEdit.obj.UserGroup.ToString ();

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
                
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion 

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            UserService userServiceObj = ServiceFactory.getUserService();
            FunctionGroupService functionGroupServiceObj = ServiceFactory.getFunctionGroupService();

            tb_User instance = new tb_User();
            tb_FunctionGroup functionGroupInstance = new tb_FunctionGroup();

            instance.Name = txtName.Text;
            instance.CNName = txtCNName.Text;
            instance.PWD = txtPWD.Text;
            instance.Tel = txtTel.Text;
            instance.UserGroup = Int32.Parse(dlGroup.SelectedValue);
            functionGroupInstance = functionGroupServiceObj.getFunctionGroupById(Int32.Parse(dlGroup.SelectedValue)).obj[0];
            instance.tb_FunctionGroup = functionGroupInstance;

            OperationResult<tb_User> result = userServiceObj.CreateUser(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            tb_FunctionGroup functionGroupInstance = new tb_FunctionGroup(); 
            FunctionGroupService functionGroupserviceObj = ServiceFactory.getFunctionGroupService();
            UserService userServiceObj = ServiceFactory.getUserService();

            tb_User entity = new tb_User();

            entity.Id = editId;
            entity.Name = txtEditName.Text;
            entity.CNName = txtEditCNName.Text;
            entity.PWD = txtEditPWD.Text;
            entity.Tel = txtEditTel.Text;
            entity.UserGroup = Int32.Parse(dlEditGroup.SelectedValue);
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            functionGroupInstance = functionGroupserviceObj.getFunctionGroupById(Int32.Parse(dlEditGroup.SelectedValue)).obj[0];
            entity.tb_FunctionGroup = functionGroupInstance; 
            OperationResult<tb_User> resultEdit = userServiceObj.ModifyUser(entity);
            
            BindGridView(); 
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            UserService userServiceObj = ServiceFactory.getUserService();
            tb_User deleteEntity = new tb_User();
            deleteEntity.Id = deleteId; 
            userServiceObj.DeleteUser(deleteEntity); 
           
            BindGridView(); 
        }

        protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            { 
                string strSelectId = e.CommandArgument.ToString(); 
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        } 

        protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUserList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
